package com.github.ecbp.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Configuration
@EnableSwagger2
public class Swagger2Config {
    /* http://{ip}:{port}/doc.html */

    @Value("${service.name}")
    private String serviceName;

    @Bean
    public Docket frontDocker() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(buildParameters())
                .groupName("front")
                .apiInfo(apiInfo("前端调用接口"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.github.ecbp." + serviceName + ".service.controller.front"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }

    @Bean
    public Docket serverDocker() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(buildParameters())
                .groupName("server")
                .apiInfo(apiInfo("服务调用接口"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.github.ecbp." + serviceName + ".service.controller.server"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }

    private ApiInfo apiInfo(String groupName) {
        return new ApiInfoBuilder()
                .title(serviceName + " 模块接口文档")
                .description(groupName + " API接口列表")
                .termsOfServiceUrl("http://ecbp.com/")
                .contact(new Contact("ecbp", "", ""))
                .version("1.0")
                .build();
    }

    private List<Parameter> buildParameters() {
        List<Parameter> pars = new ArrayList<Parameter>();
        /**
         * 设置请求头
         */
        //        ParameterBuilder tokenPar = new ParameterBuilder();
        //        tokenPar.name("Authorization").description("令牌(需登录的接口使用)").modelRef(new ModelRef("string")).parameterType("header")
        //        .required(false).
        //                defaultValue("").build();
        //        pars.add(tokenPar.build());
        /**
         * 设置请求体
         */
        ParameterBuilder pagePar = new ParameterBuilder();
        pagePar.name("page").description("当前页(需分页的接口使用)").modelRef(new ModelRef("integer")).parameterType("header").required(false).build();
        pars.add(pagePar.build());
        /**
         * 设置分页信息
         */
        ParameterBuilder limitPar = new ParameterBuilder();
        limitPar.name("limit").description("当前页条数(需分页的接口使用)").modelRef(new ModelRef("integer")).parameterType("header").required(false).build();
        pars.add(limitPar.build());
        return pars;
    }

    private List<ApiKey> securitySchemes() {
        //设置请求头信息
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("令牌-登录返回的Token", "Authorization", "header");
        result.add(apiKey);
        return result;
    }

    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build()
        );
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = new AuthorizationScope("global", "accessEverything");
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }
}